package com.lelouch.concurrency.example.atomic;

import com.lelouch.concurrency.annotation.ThreadSafe;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;

/**
 * AtomicReferenceFiledUpdater
 *
 * @author lcy
 * @create 2018-05-10 13:59
 */
@ThreadSafe
@Slf4j
public class AtomicExample4 {

    private static AtomicIntegerFieldUpdater<AtomicExample4> aifu = AtomicIntegerFieldUpdater.newUpdater(AtomicExample4.class,"count");

    @Getter
    @Setter
    public volatile int count=100;

    public static void main(String[] args) {
        AtomicExample4 atomicExample4 = new AtomicExample4();
        //若count属性值是100的话，才进行更新，更新成120
        if(aifu.compareAndSet(atomicExample4,100,120)){
            log.info("update success1,count:{}",atomicExample4.getCount());
        }

        if(aifu.compareAndSet(atomicExample4,100,120)){
            log.info("update success2,count:{}",atomicExample4.getCount());
        }else{
            log.info("update failed2!");
        }
    }
}
